import 'package:extended_text_field/extended_text_field.dart';
import 'package:flutter/material.dart';

///emoji/image text
class EmojiText extends SpecialText {
  static final prefix = "[emoticon_";
  static const String flag = "[";
  final int start;
  final bool enableGif;

  EmojiText(
    TextStyle textStyle, {
    required this.start,
    this.enableGif = false,
  }) : super(EmojiText.flag, "]", textStyle);

  @override
  InlineSpan finishText() {
    var key = toString();
    if (EmojiUitl.instance.map_common.containsKey(key)) {
      final double size = 20.0;
      return ImageSpan(AssetImage(EmojiUitl.instance.map_common[key]!), actualText: key, imageWidth: size, imageHeight: size, start: start, fit: BoxFit.contain, margin: EdgeInsets.zero);
    }
    if (enableGif) {
      if (EmojiUitl.instance.map_gif.containsKey(key)) {
        final double size = 88.0;
        return ImageSpan(AssetImage(EmojiUitl.instance.map_gif[key]!), actualText: key, imageWidth: size, imageHeight: size, start: start, fit: BoxFit.fill, margin: EdgeInsets.only(left: 2.0, right: 2.0));
      }
    }
    return TextSpan(text: toString(), style: textStyle);
  }
}

class EmojiUitl {
  final String _emoji_common = "images/emoji/common";
  final String _emoji_gif = "images/emoji/gif";
  final Map<String, String> _map_common = new Map<String, String>();
  final Map<String, String> _map_gif = new Map<String, String>();

  Map<String, String> get map_common => _map_common;

  Map<String, String> get map_gif => _map_gif;

  static EmojiUitl? _instance;

  static EmojiUitl get instance {
    if (_instance == null) _instance = new EmojiUitl._();
    return _instance!;
  }

  EmojiUitl._() {
    for (int i = 1; i < 100; i++) {
      _map_common["${EmojiText.prefix}$i]"] = "$_emoji_common/emoticon_$i.png";
    }
    for (int i = 1; i < 16; i++) {
      _map_gif["${EmojiText.prefix}gif_$i]"] = "$_emoji_gif/emoticon_gif_$i.gif";
    }
  }
}
